package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.OrderDetailBean;
import com.yanchangapp.model.bean.ProductBean;
import com.yanchangapp.model.bean.WareHouseBean;
import com.yanchangapp.model.request.EditOrderRequest;
import com.yanchangapp.model.response.EditOrderResponse;
import com.yanchangapp.ui.view.OrderEditAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-15.
 * description:订单编辑p层
 */
public class OrderEditAtPresenter extends BasePresenter<OrderEditAtView> {

    public OrderEditAtPresenter(BaseActivity context) {
        super(context);
    }

    /**
     * 加载仓库地址的数据
     */
    public void loadWareHouseData(){
        HeadApiRetrofit.getInstance().queryAllWareHouse()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<WareHouseBean>>() {
                    @Override
                    public void onNext(BaseListResponse<WareHouseBean> wareHouseBeanBaseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == wareHouseBeanBaseResponse.getCode()){
                            if (getView() != null){
                                getView().queryWareHouseSuccess(wareHouseBeanBaseResponse.getData());
                            }
                        }else {
                            if (getView() != null){
                                getView().queryWareHouseFailure(wareHouseBeanBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryWareHouseFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void editOrder(String orderId, String storeRom, double totalMoney, List<ProductBean> mBeans){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        EditOrderRequest editOrderRequest = new EditOrderRequest(orderId,storeRom,totalMoney,mBeans);
        HeadApiRetrofit.getInstance().putEditOrder(editOrderRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<EditOrderResponse>() {
                    @Override
                    public void onNext(EditOrderResponse editOrderResponse) {
                        if (AppConstant.REQUEST_SUCCESS ==editOrderResponse.getCode()){
                            if (null != getView()){
                                getView().hideLoadingDialog();
                                getView().editOrderSuccess(editOrderResponse.getData().getId());
                            }
                        }else {
                            if (null != getView()){
                                getView().hideLoadingDialog();
                                getView().editOrderFailure(editOrderResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().editOrderFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void queryOrderDetail(String orderId){
        HeadApiRetrofit.getInstance().getPurchaseOrderDetail(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OrderDetailBean>>() {
                    @Override
                    public void onNext(BaseResponse<OrderDetailBean> orderDetailBeanBaseResponse) {
                        if (orderDetailBeanBaseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (null != getView()){
                                getView().getOrderDetailSuccess(orderDetailBeanBaseResponse.getData());
                            }
                        }else {
                            if (null != getView()){
                                getView().getOrderDetailFailure(orderDetailBeanBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().getOrderDetailFailure(exception.getErrorInfo());
                        }
                    }
                });
    }




}
